//
//  internalDictController.m
//  Words2
//
//  Created by Mathieu Tozer on 16/09/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "internalDictController.h"
#import "DictionaryMO.h"
#import "dict.h"

//#import <Foundation/Foundation.h>






@implementation internalDictController

-(IBAction)addStardictDict:(id)sender{
	[dictTypeTabView selectTabViewItemWithIdentifier:@"StardictDict"]; //just switch to this if wer're not there already.
	DictionaryMO *newDict = [NSEntityDescription insertNewObjectForEntityForName:@"StardictDict" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	
	[self setCommonValues:newDict];
	//DictPrtocolDict attributes
	[newDict setValue:[stardictLocation stringValue] forKey:@"location"];
	[newDict setValue:[stardictIndexLocation stringValue] forKey:@"indexLocation"];
}
//not supported yet!

//-(IBAction)addEdictDict:(id)sender{
//	DictionaryMO *newDict = [NSEntityDescription insertNewObjectForEntityForName:@"EdictDictionary" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
//	[self setCommonValues:newDict];
//	NSString *filename = [[dictionaryName stringValue] stringByStandardizingPath];
//	NSString *homeDir = NSHomeDirectory();
//	//get the location of the dictionary
//	NSString *applicationSupportFolder =
//        [[[NSFileManager defaultManager] findSystemFolderType:kApplicationSupportFolderType forDomain:kOnSystemDisk] stringByStandardizingPath];
//	NSString *newLocation = [[NSString stringWithFormat:@"%@/%@/Words/edicts/%@", homeDir, applicationSupportFolder, filename] stringByStandardizingPath];
//	NSString *originalLocation =[[edictLocation stringValue] stringByStandardizingPath];
//
//	[newDict setValue:newLocation forKey:@"dictionaryLocation"];
//	//create a new index
//	get
//	getIndexEntry(<#PArr indexes#>,<#const char * dict#>)
//	
//	
//	[newDict setValue:
//	NSLog(@"%@", newLocation);
//	NSLog(@"%@", originalLocation);

//	if([[NSFileManager defaultManager] movePath:originalLocation toPath:newLocation handler:nil]){
//		NSLog(@"Copied to: %@", newLocation);
//	}
	
//	NSBundle *dictBundle = [NSBundle bundleWithPath:<#(NSString *)path#>
	//copy it somewhere appropritate
	//save that location in the entity
//}

-(void)setCommonValues:(DictionaryMO *)newDict{

	//Dictionary attributes
	[newDict setValue:[dictionaryName stringValue] forKey:@"name"];
	[newDict setValue:[capabilities stringValue] forKey:@"capabilities"];
	[newDict setValue:[targetLanguages stringValue] forKey:@"targetLanguages"]; //depreciated
	[newDict setValue:[pivotLanguage stringValue] forKey:@"pivotLanguage"]; //depreciated
}

#pragma mark Import / Export Dictionaries
//method which creates a bundle from the selected dictionary, archives it, and saves it as a wordsdict to a specified location.
- (IBAction)exportDictionary:(id)sender{
	int runResult;
	DictionaryMO *dict = [[self selectedObjects] objectAtIndex:0];
	NSData *data;
	NSSavePanel *panel = [NSSavePanel savePanel];
	[panel setRequiredFileType:@"wordsDict"];
	runResult = [panel runModalForDirectory:NSHomeDirectory() file:nil];
	
	/* if successful, save file under designated name */
	if (runResult == NSOKButton) {
		data = [NSKeyedArchiver archivedDataWithRootObject:dict];
		if (![data writeToFile:[panel filename] atomically:YES])
			NSLog(@"Could Note Write to File");
	}
	
	
}
- (IBAction)importDictionary:(id)sender{
	int runResult;
	DictionaryMO *aDictionary;
	NSOpenPanel *panel = [NSOpenPanel openPanel]; //get the open panel
	[panel setAllowedFileTypes:[NSArray arrayWithObject:@"wordsDict"]];
	[panel setAllowsMultipleSelection:YES];
	runResult = [panel runModalForDirectory:NSHomeDirectory() file:@""];
	if(runResult == NSOKButton){
		NSArray *filesToOpen = [panel filenames];
		int i, count = [filesToOpen count];
        for (i=0; i<count; i++) {
            NSString *aFile = [filesToOpen objectAtIndex:i];
			aDictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:aFile];
			// do something with the dictionary	
        }
	}		
}


@end
